// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ConvertUtils.cs" company="Donga CS">
//   2011
// </copyright>
// <summary>
//   The convert utils.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


namespace DTTB.Common
{
    /// <summary>
    /// The convert utils.
    /// </summary>
    public static class ConvertUtils
    {
        /// <summary>
        /// The truncate at word.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="length">The length.</param>
        /// <returns>The truncate at word.</returns>
        public static string TruncateAtWord(this string input, int? truncatedLength)
        {
            int length = truncatedLength == null ? CommonVariables.TruncatedLength : truncatedLength.Value;
            if (input == null || input.Length < length)
                return input;
            int iNextSpace = input.IndexOf(" ", length);
            return string.Format("{0}...", input.Substring(0, (iNextSpace > 0) ? iNextSpace : length).Trim());
        }

        /// <summary>
        /// The truncate at word.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>The truncate at word.</returns>
        public static string TruncateAtWord(this string input)
        {
            int length = CommonVariables.TruncatedLength;
            if (input == null || input.Length < length)
                return input;
            int iNextSpace = input.IndexOf(" ", length);
            return string.Format("{0}...", input.Substring(0, (iNextSpace > 0) ? iNextSpace : length).Trim());
        }
    }
}